//-----------------------------------------------------------------------------------------------------//
//																	                                   //
// equation_one_variable.cpp  contains implementation	of the equation_one_variable				   //
//																									   //
// author	: Eng. Ahmed Yasser																		   //
// data		: 22-01-2015																			   //
//																	                                   //
//-----------------------------------------------------------------------------------------------------//
#include "..\include\equation_one_variable.h"

equation_one_variable_t create_equation_one_variable(eq_one_variable_ptr f)
{
	equation_one_variable_t eq;
	eq.f = f;
	return eq;
}

float evaluate_equation_one_variable(const equation_one_variable_t& eq, float x)
{
	if (eq.f)
		return eq.f(x);
	else
		throw std::string("no valid evaluate function to this equation");
}

std::vector<float> evaluate_equation_one_variable(const equation_one_variable_t& eq, std::vector<float> xs)
{
	std::vector<float> res;
	for (size_t i = 0; i < xs.size(); i++)
		res.push_back(evaluate_equation_one_variable(eq, xs[i]));
	return res;
}

std::vector<ordered_pair> evaluate_equation_one_variable_on_interval(const equation_one_variable_t& eq, interval_t interval, float step)
{
	std::vector<float> xs = evaluate_interval(interval, step);
	std::vector<float> ys = evaluate_equation_one_variable(eq, xs);

	std::vector<ordered_pair> pairs;
	for (size_t i = 0; i < xs.size(); i++)
		pairs.push_back(create_ordered_pair(xs[i], ys[i]));

	return pairs;
}

void graph_equation_one_variable(const equation_one_variable_t& eq, interval_t interval, float step, float r, float g, float b, float a)
{
	// prepare the ordered pairs that identify the equation
	std::vector<ordered_pair> pairs = evaluate_equation_one_variable_on_interval(eq, interval, step);

	// create the cartesian plane and draw it
	float max_y = NEGATIVE_INFINITE;
	float min_y = POSITIVE_INFINITE;
	for (size_t i = 0; i < pairs.size(); i++)
	{
		if (pairs[i].y < min_y) min_y = pairs[i].y;
		if (pairs[i].y > max_y) max_y = pairs[i].y;
	}

	float scaler = pairs.size() / 10.0f;
	float step_x = step * scaler;
	float step_y = ((max_y - min_y) / float(pairs.size())) * scaler;
	cartesian_plane_t cp = create_cartesian_plane(0.0f, 0.0f, step_x, step_y, pairs.size() / scaler, pairs.size() / scaler);

	// draw the equation
	draw_cartesian_plane(cp, 1.0f, 1.0f, 1.0f, 1.0f);
	draw_ordered_pairs(pairs, cp, r, g, b, a);
}